// server.js
const dotenv = require('dotenv');

dotenv.config();
console.log('DB_HOST:', process.env.DB_HOST);
console.log('DB_USER:', process.env.DB_USER);
console.log('DB_PASSWORD:', process.env.DB_PASSWORD);
console.log('DB_NAME:', process.env.DB_NAME);
const app = require('./app');
const db = require('./config/db.config');
const PORT = process.env.PORT || 3000;

// 测试数据库连接
async function testDbConnection() {
    try {
        const [rows] = await db.query('SELECT 1 + 1 AS result');
        console.log('Database connection test successful:', rows);
    } catch (error) {
        console.error('Database connection test failed:', error);
        process.exit(1); // 如果连接失败，退出进程
    }
}

// 启动服务器
async function startServer() {
    await testDbConnection(); // 先测试数据库连接
    app.listen(PORT, () => {
        console.log(`Server is running on port ${PORT}`);
    });
}

startServer();
